import 'package:flutter/material.dart';

import '../example/index.dart';
import 'category/category.dart';
import 'home/home.dart';
import 'knowledge/knowledge.dart';
import 'my/my.dart';

class Tabber extends StatefulWidget {
  const Tabber({super.key});

  @override
  State<Tabber> createState() => _TabberState();
}

class _TabberState extends State<Tabber> {
  int _current = 0;
  List<Map<String, Object>> items = [
    {'icon': Icons.home, 'label': '首页'},
    {'icon': Icons.turned_in_not, 'label': '示例'},
    {'icon': Icons.category, 'label': '分类'},
    {'icon': Icons.school, 'label': '知识'},
    {'icon': Icons.account_circle, 'label': '我的'},
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: IndexedStack(
          index: _current,
          children: [
            HomePage(),
            ExamplePage(),
            CategoryPage(),
            KnowledgePage(),
            MyPage(),
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: _tabberItems(),
        fixedColor: Colors.blue,
        type: BottomNavigationBarType.fixed,
        currentIndex: _current,
        onTap: (index) {
          setState(() {
            _current = index;
          });
        },
      ),
    );
  }

  List<BottomNavigationBarItem> _tabberItems() {
    List<BottomNavigationBarItem> items = [];
    for (var item in this.items) {
      items.add(BottomNavigationBarItem(icon: Icon(item['icon'] as IconData), label: item['label'] as String));
    }
    return items;
  }
}
